<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateGoldWastesTable extends Migration
{
    public function up()
    {
        Schema::create('gold_wastes', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->default(0)->index();
            $table->unsignedBigInteger('gold_seed_id')->default(0)->index();
            $table->unsignedBigInteger('gold_round_id')->default(0)->index();
            $table->unsignedBigInteger('gold_product_id')->default(0)->index();
            $table->unsignedBigInteger('product_id')->default(0)->index();
            $table->unsignedBigInteger('product_sku_id')->default(0)->index();
            $table->unsignedBigInteger('up_gold_record_id')->default(0)->index()->comment('resold from gold record');
            $table->unsignedInteger('round')->default(0);
            $table->string('gold_seed_title')->nullable();
            $table->string('product_title')->nullable();
            $table->string('product_cover')->nullable();
            $table->string('sku_name')->nullable();
            $table->string('sku_pic')->nullable();
            $table->string('sku_attr_value_index')->nullable();
            $table->text('sku_attr_value')->nullable();

            $table->decimal('resell_price', 10, 2)->default(0)->comment('tomorrow resell price of current record');
            $table->decimal('record_price', 10, 2)->default(0)->comment('today current record buy price');
            $table->decimal('record_beans', 10, 2)->default(0)->comment('beans for current record reselling fee');
            $table->decimal('record_prize', 10, 2)->default(0)->comment('profit to parent record user');

            $table->dateTime('closed_at')->nullable()->index();
            $table->dateTime('paid_at')->nullable()->index();
            $table->string('paid_method')->nullable();

            $table->dateTime('to_resell_applied_at')->nullable();
            $table->dateTime('to_split_applied_at')->nullable();
            $table->dateTime('to_take_applied_at')->nullable();
            $table->unsignedInteger('splits_amount')->default(0);
            $table->unsignedInteger('splits_settled_amount')->default(0);

            $table->boolean('is_record_handled')->default(false)->comment('if user handled reselling, splitting, taking records fully');

            $table->dateTime('settled_at')->nullable()->index()->comment('settle up record reselling or split money');
            $table->dateTime('rewarded_at')->nullable()->index();
            $table->dateTime('finished_at')->nullable()->index();

            $table->softDeletes();
            $table->timestamps();

            $table->index(['created_at']);
        });
    }

    public function down()
    {
        Schema::dropIfExists('gold_wastes');
    }
}
